/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package com.linkwithweb.products.swiftui.export.excel;


import java.math.BigDecimal;
import java.sql.Types;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * Used to apply Styles in Excel Sheet
 * 
 * @author ashwin
 * 
 */
public class Styles {
	public static Map<String, CellStyle> styles;

	// creating styles for int, double, string and hyperlink
	public static Map getStyles(HSSFWorkbook workbook) {
		styles = new HashMap<String, CellStyle>();

		CellStyle style;
		// for title
		Font titleFont = workbook.createFont();
		// titleFont.setFontHeightInPoints((short) 10);
		titleFont.setColor(IndexedColors.WHITE.getIndex());
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style = workbook.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setFont(titleFont);
		style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		styles.put("title", style);

		// for hyperlink style
		Font hyperFont = workbook.createFont();
		hyperFont.setColor(IndexedColors.DARK_BLUE.getIndex());
		hyperFont.setUnderline(Font.U_SINGLE);
		style = workbook.createCellStyle();
		style.setFont(hyperFont);
		styles.put("hyperlink", style);

		// for date
		style = workbook.createCellStyle();
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		styles.put("date", style);

		// for heading
		Font headFont = workbook.createFont();
		headFont.setColor(IndexedColors.GREEN.getIndex());
		headFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headFont.setFontHeightInPoints((short) 20);
		style = workbook.createCellStyle();
		style.setFont(headFont);
		style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		styles.put("header", style);

		return styles;
	}

	/**
	 * Type is type from Java.sql.Types
	 * 
	 * @param type
	 * @param value
	 * @param cell
	 */
	public static void addStyleAndValue(int type, String value, HSSFCell cell) {

		/**
		 * for 1 - integer 2 - Double 3 - Date 4 - Hyper link 4 - String
		 */
		// handling for null value
		if (value == null) {
			cell.setCellValue("");
			return;
		}
		switch (type) {
		case Types.INTEGER:
		case Types.SMALLINT:
			// in case its integer just type cast and set the value
			//cell.setCellValue(Integer.parseInt(value));
			cell.setCellValue(value);
			break;
		case Types.DOUBLE:
		case Types.DECIMAL:
		case Types.FLOAT:
			// in case it is double just type cast and set the value
			//cell.setCellValue(new BigDecimal(value).toPlainString());
			cell.setCellValue(value);
			break;
		case Types.DATE:
		case Types.TIME:
		case Types.TIMESTAMP:
			try {
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				if (value.length() > 11) {
					String date = value.substring(0, 11);
					Date datee = dateFormat.parse(date);
					cell.setCellValue(datee);
					cell.setCellStyle(styles.get("date"));
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			break;
		case Types.VARCHAR:
		case Types.LONGVARCHAR:
			// in case it is hyperlink then set the font style and even the link
			cell.setCellValue(value);
			if (isHyperlink(value)) {
				HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
				// setting the address to the link
				link.setAddress(value);
				// setting the link to the cell
				cell.setHyperlink(link);
				cell.setCellStyle(styles.get("hyperlink"));
			} else {
				cell.setCellValue(value);
			}
			break;

		case Types.BOOLEAN:
		case Types.BIT:
		case Types.CHAR:
			// in case it is something else then set the value directly in
			// string form
			cell.setCellValue(value);
			break;

		case Types.BIGINT:
		case Types.NUMERIC:
			// in case it is big int then we need to consider it as long
			cell.setCellValue(Double.parseDouble(value));
			break;
		default:
			break;
		}
		// return style;
	}

	/**
	 * 
	 * @param the
	 *            string is given as input to check for hyperlink type
	 * @return varchar return type is returned
	 */

	public static boolean isHyperlink(String value) {
		String hyper = "http://";
		if (value.length() > 7) {
			String val = value.substring(0, 7);
			if (val.equals(hyper)) {
				// if hyperlink return 3
				return true;
			}
		}
		return false;
	}
}
